//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "StochasticProcessArray.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (Cephei::Core::IVector<Cephei::QL::IStochasticProcess1D^>^ processes, Cephei::QL::Math::IMatrix^ correlation) : CStochasticProcess(CStochasticProcessArray::typeid)
{
    CMatrix^ _Ccorrelation;
    try
    {
#ifdef HANDLE
        _phStochasticProcessArray = NULL;
#endif
        processes ->Lock ();
        INativeVector<Cephei::QL::IStochasticProcess1D^>^ _NCIprocesses = processes->getFeature (NativeFeature::shared_ptr);
        CStochasticProcess1DVector^ _NCprocesses = safe_cast<CStochasticProcess1DVector^>(_NCIprocesses);
        std::vector<boost::shared_ptr<QuantLib::StochasticProcess1D> >& _processes = static_cast<std::vector<boost::shared_ptr<QuantLib::StochasticProcess1D> >&> (_NCprocesses->GetShared ());
        _Ccorrelation = safe_cast<CMatrix^> (correlation);
        _Ccorrelation->Lock();
        QuantLib::Matrix& _correlation = static_cast<QuantLib::Matrix&> (_Ccorrelation->GetReference ()); 
        _ppStochasticProcessArray = new boost::shared_ptr<QuantLib::StochasticProcessArray> (new QuantLib::StochasticProcessArray ( _processes,  _correlation ));
        SetStochasticProcess (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (processes != nullptr) processes->Unlock();    //not optional
        if (_Ccorrelation != nullptr) _Ccorrelation->Unlock();
    }
}
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (boost::shared_ptr<QuantLib::StochasticProcessArray>& childNative, Object^ owner) : CStochasticProcess(CStochasticProcessArray::typeid)
{
#ifdef HANDLE
	_phStochasticProcessArray = NULL;
#endif
	_ppStochasticProcessArray = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
}
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (QuantLib::StochasticProcessArray& childNative, Object^ owner) : CStochasticProcess(CStochasticProcessArray::typeid)
{
#ifdef HANDLE
	_phStochasticProcessArray = NULL;
#endif
	_ppStochasticProcessArray = new boost::shared_ptr<QuantLib::StochasticProcessArray> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
    _StochasticProcessArrayOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (CStochasticProcessArray^ copy) : CStochasticProcess(CStochasticProcessArray::typeid)
{
#ifdef HANDLE
	_phStochasticProcessArray = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStochasticProcessArray = new boost::shared_ptr<QuantLib::StochasticProcessArray> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
    }
}
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (PLATFORM::Type^ t) : CStochasticProcess(CStochasticProcessArray::typeid)
{
#ifdef HANDLE
	_phStochasticProcessArray = NULL;
#endif
	if (!t->IsSubclassOf(CStochasticProcessArray::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (QuantLib::Handle<QuantLib::StochasticProcessArray>& childNative, Object^ owner)  : CStochasticProcess(CStochasticProcessArray::typeid)
{
	_phStochasticProcessArray = &childNative;
	_ppStochasticProcessArray = &static_cast<boost::shared_ptr<QuantLib::StochasticProcessArray>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
    _StochasticProcessArrayOwner = owner;
}
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (QuantLib::Handle<QuantLib::StochasticProcessArray> childNative)  : CStochasticProcess(CStochasticProcessArray::typeid)
{
	_phStochasticProcessArray = &childNative;
	_ppStochasticProcessArray = &static_cast<boost::shared_ptr<QuantLib::StochasticProcessArray>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CStochasticProcessArray::CStochasticProcessArray (QuantLib::StochasticProcessArray childNative)  : CStochasticProcess(CStochasticProcessArray::typeid)
{
#ifdef HANDLE
	_phStochasticProcessArray = NULL;
#endif
	_ppStochasticProcessArray = new boost::shared_ptr<QuantLib::StochasticProcessArray> (new QuantLib::StochasticProcessArray (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppStochasticProcessArray));
}
#endif

Cephei::QL::Processes::CStochasticProcessArray::~CStochasticProcessArray ()
{
    if (_ppStochasticProcessArray != NULL)
    {
	    delete _ppStochasticProcessArray;
        _ppStochasticProcessArray = NULL;
    }
}
Cephei::QL::Processes::CStochasticProcessArray::!CStochasticProcessArray ()
{
    if (_ppStochasticProcessArray != NULL)
    {
	    delete _ppStochasticProcessArray;
    }
}
QuantLib::StochasticProcessArray& Cephei::QL::Processes::CStochasticProcessArray::GetReference ()
{
    if (_ppStochasticProcessArray == NULL) throw REFNEW NativeNullException ();
	return **_ppStochasticProcessArray;
}
boost::shared_ptr<QuantLib::StochasticProcessArray>& Cephei::QL::Processes::CStochasticProcessArray::GetShared ()
{
    if (_ppStochasticProcessArray == NULL) throw REFNEW NativeNullException ();
	return *_ppStochasticProcessArray;
}
QuantLib::StochasticProcessArray* Cephei::QL::Processes::CStochasticProcessArray::GetPointer ()
{
    if (_ppStochasticProcessArray == NULL) throw REFNEW NativeNullException ();
	return &**_ppStochasticProcessArray;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::StochasticProcessArray>& Cephei::QL::Processes::CStochasticProcessArray::GetHandle ()
{
	if (_phStochasticProcessArray == NULL)
	{
		_phStochasticProcessArray = new Handle<QuantLib::StochasticProcessArray> (*_ppStochasticProcessArray);
	}
	return *_phStochasticProcessArray;
}
#endif
bool Cephei::QL::Processes::CStochasticProcessArray::HasNative () 
{
	return (_ppStochasticProcessArray != NULL);
}

Cephei::QL::IStochasticProcess1D^ Cephei::QL::Processes::CStochasticProcessArray::Process (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	boost::shared_ptr<QuantLib::StochasticProcess1D>& _rv = (boost::shared_ptr<QuantLib::StochasticProcess1D>&)(*_ppStochasticProcessArray)->process ( _i );   
        Cephei::QL::CStochasticProcess1D^ _nrv = REFNEW Cephei::QL::CStochasticProcess1D (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Processes::CStochasticProcessArray::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppStochasticProcessArray)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CStochasticProcessArray::Time (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppStochasticProcessArray)->time ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IStochasticProcessArray^ Cephei::QL::Processes::CStochasticProcessArray_Factory::Create (Cephei::Core::IVector<Cephei::QL::IStochasticProcess1D^>^ processes, Cephei::QL::Math::IMatrix^ correlation)
{
    return REFNEW CStochasticProcessArray ( processes,  correlation);
}
